package com.cyou.qb.common.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.cyou.nad.datastore.query.JdbcPage;
import com.cyou.nad.datastore.query.JdbcPageDao;
import com.cyou.qb.common.dao.QbAdminDao;
import com.cyou.qb.common.domain.QiangbaAdmin;
import com.cyou.qb.common.service.AccountService;
import com.cyou.qb.constants.Constants;
@Service
public class AccountServiceImpl extends BaseSerivceImpl implements AccountService {

	@Autowired
	private QbAdminDao qbAdminDao;
	@Autowired
	private JdbcPageDao jdbcPageDao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public JdbcPage pageQueryAccount(String loginName, String dept, String name, String type, String valid, int pageNo,
			int pageSize) {
		String sql="select 	id, login_name, pwd, name, dept,type,status,valid,last_time,last_ip,create_date "
				+" from t_sys_admin "
				+ " where 1=1  and status = 1";
		List<Object> param = new ArrayList<Object>(3); 
		if(StringUtils.isNotBlank(loginName)){
			sql+=" and login_name like ?";
			param.add("%"+loginName+"%");
		}
		if(StringUtils.isNotBlank(dept)){
			sql+=" and dept like ?";
			param.add("%"+dept+"%");
		}
		if(StringUtils.isNotBlank(name)){
			sql+=" and name like ?";
			param.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(type)){
			sql+=" and type = ?";
			param.add(type);
		}
		if(StringUtils.isNotBlank(valid)){
			sql+=" and valid = ?";
			param.add(valid);
		}
		sql+=" order by  create_date desc ";
		return jdbcPageDao.pageQuery(sql, pageNo, pageSize, param.toArray());
	}


	/**
	 * Description 保存一个账户
	 * @author lidongwei
	 * @throws Exception 
	 * @since 2012-9-20 下午3:59:30
	 * @see TODOsome class
	 */
	@Override
	public int saveOneAdmin(String loginName, String dept, String userName, String adminType) {
		QiangbaAdmin qiangbaAdmin = new QiangbaAdmin();
		qiangbaAdmin.setLoginName(loginName);
		qiangbaAdmin.setDept(dept);
		qiangbaAdmin.setName(userName);
		qiangbaAdmin.setType(adminType);
		qiangbaAdmin.setCreateDate(new Date());
		qiangbaAdmin.setStatus(1);//调用constant默认值
		qiangbaAdmin.setValid(1);//调用constant默认值
		qiangbaAdmin.setPassword(Constants.ADMIN_USER_DEFAULT_PWD);//MD5加密后密码
		qbAdminDao.saveOneAdmin(qiangbaAdmin);
		getServiceContext().addLog(loginName, qiangbaAdmin.getAdminId(), "t_sys_admin", "添加管理员", new Object[]{qiangbaAdmin}, null);
		return -1;
	}

	/**
	 * Description 删除一个账户
	 * @author lidongwei
	 * @since 2012-9-20 下午3:59:30
	 * @see TODOsome class
	 */
	@Override
	public int removeAdmin(List<String> adminIds) {
		if (adminIds != null && adminIds.size() > 0) {
			for (int i = 0; i < adminIds.size(); i++) {
				Integer adminId = Integer.valueOf(adminIds.get(i));
				qbAdminDao.removeOneAdmin(adminId);
				getServiceContext().addLog(adminId.toString(), adminId, "t_sys_admin", "删除管理员", new Object[]{"0"}, new Object[]{"1"});
			}
		}
		return 0;
	}

	

	/**
	 * Description 更新一个账户
	 * @author lidongwei
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @since 2012-9-20 下午3:59:30
	 * @see TODOsome class
	 */
	@Override
	public void updateAccount(Integer adminId, String dept, String userName, String adminType) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		QiangbaAdmin qiangbaAdmin = null;
		if (StringUtils.isNotEmpty(adminType) && StringUtils.isNotEmpty(userName)
				&& StringUtils.isNotEmpty(dept)) {
			qiangbaAdmin = qbAdminDao.findOneAmind(adminId);
			QiangbaAdmin org =new QiangbaAdmin();
				PropertyUtils.copyProperties(org, qiangbaAdmin);
			qiangbaAdmin.setDept(dept);
			qiangbaAdmin.setName(userName);
			qiangbaAdmin.setType(adminType);
			getServiceContext().addLog(qiangbaAdmin.getLoginName(), org.getAdminId(), "t_sys_admin", "修改管理员", new Object[]{qiangbaAdmin}, new Object[]{org});
			qbAdminDao.updateAccount(qiangbaAdmin);
		}
	}



	/**
	 * 
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6 下午7:03:52
	 * @see TODOsome class
	 */
	@Override
	public boolean checkLoginNameExist(String loginName) {
		Assert.hasText(loginName,"登录名称不能为空");
		String sql="select count(id) from t_sys_admin where login_name=? and status=1";
		int i= jdbcTemplate.queryForInt(sql,new Object[]{loginName});
		return i>0;
	}


	/**
	 * 
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6 下午7:03:57
	 * @see TODOsome class
	 */
	@Override
	public void updateLockStatus(List<String> adminIds, String pTag) {
		if(adminIds!=null&&adminIds.size()>0)
		{
		for (int i = 0; i < adminIds.size(); i++) {
			String adminId = adminIds.get(i);
			if (StringUtils.isNumeric(adminId) && StringUtils.isNotEmpty(pTag) ) {
				qbAdminDao.updateOneP(Integer.valueOf(adminId), Integer.valueOf(pTag));
				getServiceContext().addLog(adminId, Integer.valueOf(adminId), "t_sys_admin", "修改管理员锁定状态", new Object[]{pTag}, new Object[]{pTag.equals("1")?"0":"1"});
			}
		}
		}
		
	}


	/**
	 * 
	 * Description
	 * @author lidongwei
	 * @since 2012-10-6 下午7:04:00
	 * @see TODOsome class
	 */
	@Override
	public void updatePassword(Integer adminId, String password) {
		Assert.hasText(password);
		String sql=" update t_sys_admin set pwd = ? where id = ?";
		jdbcTemplate.update(sql, new Object[]{password,adminId});
		getServiceContext().addLog(adminId.toString(), adminId, "t_sys_admin", "修改管理员密码", new Object[]{password}, new Object[]{""});
	}


	/**
	 * Description 查询用户是否存在
	 * @author lidongwei
	 * @since 2012-9-23 下午3:24:10
	 * @see TODOsome class
	 */
	@Override
	public QiangbaAdmin checkLogin(String loginName, String password) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("loginName", loginName);
		map.put("password", password);
		QiangbaAdmin qiangbaAdmin  = qbAdminDao.checkLogin(map);
		if(qiangbaAdmin==null||qiangbaAdmin.getStatus()==0||qiangbaAdmin.getValid()==0)
		{
			return null;
		}
		return qiangbaAdmin;
	}
	
	
	/**
	 * c重置密码
	 * Description  RestorePassword ：1 LockAccount:2 unLockAccount:3
	 * @author lidongwei
	 * @since 2012-9-20 下午3:59:30
	 * @see TODOsome class
	 */
	@Override
	public void updateResetPassword(List<String> adminIds) {
		if(adminIds!=null&&adminIds.size()>0)
		{
		for (int i = 0; i < adminIds.size(); i++) {
			String adminId = adminIds.get(i);
			if (StringUtils.isNumeric(adminId)) {
				qbAdminDao.resetPassword(Integer.valueOf(adminId));
				getServiceContext().addLog(adminId, Integer.valueOf(adminId), "t_sys_admin", "重置管理员密码", new Object[]{Constants.ADMIN_USER_DEFAULT_PWD}, new Object[]{""});
			}
		}
		}

	}


	/**
	 * Description
	 * @author lidongwei
	 * @since 2012-10-12 上午10:56:03
	 * @see TODOsome class
	 */
	@Override
	public void updateOneAccount(QiangbaAdmin qiangbaAdmin) {
		qbAdminDao.updateBase(qiangbaAdmin);
	}
}
